package com.kittlen.managementSystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kittlen.managementSystem.entity.MySqlLimit;
import com.kittlen.managementSystem.entity.Result;
import com.kittlen.managementSystem.entity.VCoach;
import com.kittlen.managementSystem.service.IVCoachService;
import com.kittlen.managementSystem.utils.ObjectUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 教练(包含车辆) 前端控制器
 * </p>
 *
 * @author kittlen
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/vCoach")
public class VCoachController {

    @Resource
    IVCoachService ivCoachService;

    @RequestMapping("/selects")
    public Result selects(VCoach vCoach, @RequestParam(required = false, defaultValue = "0") int page,
                          @RequestParam(required = false, defaultValue = "0") int limit, @RequestParam(required = false,defaultValue = "0")int isFree
            , @RequestParam(required = false,defaultValue = "0")int possessVehicle){
        Result result=new Result();
        QueryWrapper<VCoach> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNoEmpty(vCoach.getCoachId())){
            queryWrapper.like("coach_id", vCoach.getCoachId());
        }
        if(ObjectUtil.isNoEmpty(vCoach.getFullname())){
            queryWrapper.like("fullname", vCoach.getFullname());
        }
        if(ObjectUtil.isNoEmpty(vCoach.getAgender())){
            queryWrapper.like("agender", vCoach.getAgender());
        }
        if(ObjectUtil.isNoEmpty(vCoach.getPhone())){
            queryWrapper.like("phone", vCoach.getPhone());
        }
        if(ObjectUtil.isNoEmpty(vCoach.getEmail())){
            queryWrapper.like("email", vCoach.getEmail());
        }
        if(ObjectUtil.isNoEmpty(vCoach.getIsEnd())){//是否辞职
            queryWrapper.eq("is_end", vCoach.getIsEnd());
        }
        if(ObjectUtil.isNoEmpty(vCoach.getIsStartChoice())){//是否开始被选
            queryWrapper.eq("is_start_choice", vCoach.getIsStartChoice());
        }
        if(ObjectUtil.isNoEmpty(vCoach.getTeachingStage())&&vCoach.getTeachingStage()!=0){//主要负责的教学科目
            queryWrapper.eq("teaching_stage", vCoach.getTeachingStage());
        }
        if (page!=-1) {
            MySqlLimit mySqlLimit=new MySqlLimit(page,limit);
            queryWrapper.last("LIMIT "+mySqlLimit.getOffset()+","+mySqlLimit.getRows());
        }
        if(ObjectUtil.isNoEmpty(isFree)&&isFree!=0){//当前未全部分派有学员
            queryWrapper.apply("choice_count < choice_count_max");
        }
        if(ObjectUtil.isNoEmpty(possessVehicle)&&possessVehicle!=0){//查询没有分派到车的教练
            queryWrapper.isNull("vehicle_id");
        }
        List<VCoach> list=ivCoachService.list(queryWrapper);
        result.setData(list);
        queryWrapper.last("");
        result.setCount(ivCoachService.count(queryWrapper));
        return result;
    }

}
